<template>
  <div class="p-4">
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #tableTitle>
        <a-button type="primary" @click="handleCreate"> 新增字典 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:radius-setting-outlined',
                tooltip: '字典配置',
                onClick: handleOpenItem.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <DictItemDrawer @register="registerItemDrawer" @success="handleSuccess" />
    <DictModal @register="registerSaveModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive } from 'vue';
  import { BasicTable, useTable, BasicColumn, TableAction } from '/@/components/Table';
  import { columns, searchFormSchema } from './dict.data';
  import { list, deleteDict } from '/@/api/system/dict/dict.api';
  import DictItemDrawer from './item/DitcItemDrawer.vue';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import DictModal from './DictModal.vue';

  export default defineComponent({
    components: { BasicTable, TableAction, DictModal, DictItemDrawer },
    setup() {
      const searchInfo = reactive<Recordable>({});
      const [registerItemDrawer, { openDrawer: openItemDrawer }] = useDrawer();
      const [registerSaveModal, { openModal: openSaveModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        api: list,
        columns: columns,
        useSearchForm: true,
        bordered: true,
        showTableSetting: true,
        showIndexColumn: false,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: undefined,
        },
      });
      function handleEdit(record: Recordable) {
        openSaveModal(true, {
          record,
          isUpdate: true,
        });
      }
      async function handleDelete(record: Recordable) {
        await deleteDict({ id: record.id }, reload);
      }
      function handleOpenItem(record: Recordable) {
        openItemDrawer(true, {
          id: record.id,
        });
      }
      function handleSuccess() {
        reload();
      }
      function handleCreate() {
        openSaveModal(true, {
          isUpdate: false,
        });
      }
      return {
        registerTable,
        handleEdit,
        handleDelete,
        searchInfo,
        handleSuccess,
        handleOpenItem,
        handleCreate,
        registerSaveModal,
        registerItemDrawer,
      };
    },
  });
</script>
